/** webpack生产环境配置 @author dongls */

const path = require('path');

const { merge } = require('webpack-merge');
const baseConfig = require('./webpack.base.conf')

const HtmlWebpackPlugin = require('html-webpack-plugin');

// https://github.com/webpack-contrib/mini-css-extract-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const CleanWebpackPlugin = require('clean-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')

const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');

// const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
// https://www.npmjs.com/package/webpack-bundle-analyzer
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

delete baseConfig.entry

module.exports = merge(baseConfig, {
  mode: 'development',
  entry: {
    'app':'./src/index.js'
  },
  cache: {
    type: 'filesystem',
    allowCollectingMemory: true,
  },
  output: {
    publicPath: '/',
    filename: 'chunks/[name].[chunkhash:8].js',
    chunkFilename: 'chunks/[name].[chunkhash:8].js'
  },
  resolve: {
    alias: {
      'app.config': path.resolve(__dirname, '../src/config/development.js'),
      // 'vue$': 'vue/dist/vue.runtime.esm.js',
    }
  },
  optimization: {
    // runtimeChunk: true,
    splitChunks: {
      cacheGroups: {
        common: {
          name: 'common',
          test(module, chunks){
            let conditions = [
              /[\\/]src[\\/](assets|common|component|config|directive|filter|mixin|platform|util)/,
              /[\\/]node_modules[\\/]viewerjs[\\/]/
            ];
            
            if(module.nameForCondition){
              let name = module.nameForCondition();
              return conditions.some(reg => reg.test(name))
            }
            
            return false;
          },
          priority: 20,
          chunks: 'initial'
        },
        vendors: {
          name: 'vendors',
          // https://github.com/webpack/webpack/blob/master/lib/optimize/SplitChunksPlugin.js#L283 
          test: /[\\/]node_modules[\\/]/,
          priority: 100,
          chunks: 'initial'
        }
      }
    }
  },
  performance: {
    maxEntrypointSize: 400000,
    maxAssetSize: 100000,
  },
  plugins: [
    
    new CleanWebpackPlugin(['dist'], {
      root: path.join(__dirname, '..')
    }),
    
    new MiniCssExtractPlugin({
      filename: 'css/[name].[chunkhash:8].css',
      chunkFilename: 'css/[name].[chunkhash:8].css',
      ignoreOrder: true
    }),
    
    new OptimizeCSSPlugin(),
    
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './index.html',
      inject: true
    }),
    
  ]
});